package br.quest;

import br.quest.activities.OptionsActivity;
import br.quest.activities.SelectRouteActivity;
import br.quest.activities.StatisticsActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public abstract class AbstractActivity extends Activity {

    protected static final int ACTION_OPTIONS = 0;
    protected static final int ACTION_STATISTICS = 1;
    protected static final int ACTION_NEW_ROUTE = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(getLayoutId());
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
    }

    /**
     * Select the icons to be shown on the action bar.
     * 
     * @param icons An array containing one of the icon's IDs provided on this class. Example: ACTION_STATISTICS
     */
    protected void setActionBar(int[] icons) {
        int qty = icons.length;
        ImageView oldView = null;
        for (int i = 0; i < qty; i++) {
            ImageView img = new ImageView(this);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            img.setId(i + 1);
            img.setImageResource(getActionButtonCorrectId(icons[i]));
            if (i == 0) {
                params.rightMargin = 5;
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            } else {
                params.rightMargin = 10;
                params.addRule(RelativeLayout.LEFT_OF, oldView.getId());
            }
            img.setLayoutParams(params);
            img.setOnClickListener(createActionBarListener(icons[i]));
            oldView = img;
            RelativeLayout layout = (RelativeLayout) findViewById(R.id.titleLayout);
            layout.addView(img);
        }
    }

    private OnClickListener createActionBarListener(int option) {
        final Intent intent;
        switch (option) {
        case ACTION_OPTIONS:
            intent = new Intent(this, OptionsActivity.class);
            break;
        case ACTION_STATISTICS:
            intent = new Intent(this, StatisticsActivity.class);
            break;
        case ACTION_NEW_ROUTE:
            intent = new Intent(this, SelectRouteActivity.class);
            break;
        default:
            intent = null;
        }
        OnClickListener listener = new OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        };
        return listener;
    }

    private int getActionButtonCorrectId(int option) {
        switch (option) {
        case ACTION_OPTIONS:
            return R.drawable.action_options;
        case ACTION_STATISTICS:
            return R.drawable.action_stats;
        case ACTION_NEW_ROUTE:
            return R.drawable.action_route;
        default:
            return 0;
        }

    }

    protected abstract int getLayoutId();
}
